package com.duanlian.baidumapdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;

/**
 * 地图定位
 */
public class MapActivity extends Activity {
    // 定位相关
    LocationClient mLocClient;
    //定位监听
    public MyLocationListenner myListener = new MyLocationListenner();
    MapView mMapView = null;
    BaiduMap mBaiduMap;
    boolean isFirstLoc = true; // 是否首次定位
    BDLocation mlocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_map);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);//普通地图
//        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);//卫星地图
//        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NONE);//空白地图
//        mBaiduMap.setTrafficEnabled(true);//实时路况图
//        mBaiduMap.setBaiduHeatMapEnabled(true);//城市热力图
        mBaiduMap.setIndoorEnable(true);//打开室内图，默认为关闭状态
//        ////////////////////////
//        mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
//                mCurrentMode, true, mCurrentMarker,
//                accuracyCircleFillColor, accuracyCircleStrokeColor));
//
//        /////////////////////////
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        // 定位初始化
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        option.setAddrType("all");
        mLocClient.setLocOption(option);
        mLocClient.start();
    }

    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            mlocation = location;

            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(mlocation.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(mlocation.getLatitude())
                    .longitude(mlocation.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(ll).zoom(18.0f);
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
    }

    @Override
    protected void onPause() {
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
        mBaiduMap.setMyLocationEnabled(false);/////////////////
        super.onPause();
    }

    @Override
    protected void onResume() {
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
        mBaiduMap.setMyLocationEnabled(true);//////////////////
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        // 退出时销毁定位
        mLocClient.stop();
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
    }

    /**
     * 发送按钮的点击事件
     */
    public void sendMessage(View v) {

        if (mlocation == null || mMapView == null) {
            Toast.makeText(MapActivity.this, "点击了发送按钮", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent();
        //纬度
        intent.putExtra("getLatitude", mlocation.getLatitude()+"");
        //经度
        intent.putExtra("getLongitude", mlocation.getLongitude()+"");
        //地址
        intent.putExtra("getAddress", mlocation.getAddrStr());
        setResult(RESULT_OK, intent);
        finish();
    }
//    /////////////////////////////
//    //位置信息监听
//    @Override
//    public void onReceiveLocation(BDLocation bdLocation) {
//        // 定位接口可能返回错误码,要根据结果错误码,来判断是否是正确的地址;
//        int locType = bdLocation.getLocType();
//        switch (locType) {
//            case BDLocation.TypeCacheLocation:
//            case BDLocation.TypeOffLineLocation:
//            case BDLocation.TypeGpsLocation:
//            case BDLocation.TypeNetWorkLocation:
//
//                radius = bdLocation.getRadius();
//                user_latitude = bdLocation.getLatitude();
//                user_longitude = bdLocation.getLongitude();
//                mCurrentX = bdLocation.getDirection();
//
//                MyLocationData data = new MyLocationData.Builder()
//                        .accuracy(radius)
//                        .direction(mCurrentX)
//                        .latitude(user_latitude)
//                        .longitude(user_longitude)
//                        .build();
//                map.setMyLocationData(data);
//                MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, null);
//                map.setMyLocationConfigeration(config);
//
//                break;
//            default:
//                String s = bdLocation.getLocTypeDescription();
//                break;
//        }
//    }
//    ////////////////////////////////
}
